home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / bin / sniffer.py < prev    next >
Encoding:
Python Source  |  2006-06-30  |  2.1 KB  |  78 lines

  1. #!/usr/bin/python
  2. # Copyright (c) 2003 CORE Security Technologies
  3. #
  4. # This software is provided under under a slightly modified version
  5. # of the Apache Software License. See the accompanying LICENSE file
  6. # for more information.
  7. #
  8. # $Id: sniffer.py,v 1.3 2003/10/27 17:36:56 jkohen Exp $
  9. #
  10. # Simple packet sniffer.
  11. #
  12. # This packet sniffer uses a raw socket to listen for packets
  13. # in transit corresponding to the specified protocols.
  14. #
  15. # Note that the user might need special permissions to be able to use
  16. # raw sockets.
  17. #
  18. # Authors:
  19. #  Gerardo Richarte <gera@coresecurity.com>
  20. #  Javier Kohen <jkohen@coresecurity.com>
  21. #
  22. # Reference for:
  23. #  ImpactDecoder.
  24.  
  25. from select import select
  26. import socket
  27. import sys
  28.  
  29. import impacket
  30. from impacket import ImpactDecoder
  31.  
  32. DEFAULT_PROTOCOLS = ('icmp', 'tcp', 'udp')
  33.  
  34. if len(sys.argv) == 1:
  35.     toListen = DEFAULT_PROTOCOLS
  36.     print "Using default set of protocols. A list of protocols can be supplied from the command line, eg.: %s <proto1> [proto2] ..." % sys.argv[0]
  37. else:
  38.     toListen = sys.argv[1:]
  39.  
  40. # Open one socket for each specified protocol.
  41. # A special option is set on the socket so that IP headers are included with
  42. # the returned data.
  43. sockets = []
  44. for protocol in toListen:
  45.     try:
  46.         protocol_num = socket.getprotobyname(protocol)
  47.     except socket.error:
  48.         print "Ignoring unknown protocol:", protocol
  49.         toListen.remove(protocol)
  50.         continue
  51.     s = socket.socket(socket.AF_INET, socket.SOCK_RAW, protocol_num)
  52.     s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  53.     sockets.append(s)
  54.  
  55. if 0 == len(toListen):
  56.     print "There are no protocols available."
  57.     sys.exit(0)
  58.  
  59. print "Listening on protocols:", toListen
  60.  
  61. # Instantiate an IP packets decoder.
  62. # As all the packets include their IP header, that decoder only is enough.
  63. decoder = ImpactDecoder.IPDecoder()
  64.  
  65. while len(sockets) > 0:
  66.     # Wait for an incoming packet on any socket.
  67.     ready = select(sockets, [], [])[0]
  68.     for s in ready:
  69.         packet = s.recvfrom(4096)[0]
  70.         if 0 == len(packet):
  71.             # Socket remotely closed. Discard it.
  72.             sockets.remove(s)
  73.             s.close()
  74.         else:
  75.             # Packet received. Decode and display it.
  76.             packet = decoder.decode(packet)
  77.             print packet
  78.